<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 https://git.oschina.net/honoryao All rights reserved.
// +----------------------------------------------------------------------
// | Author: honoryao <2463523099@qq.com>-2017/8/16
// +----------------------------------------------------------------------
// | ${NAMESPACE}-User
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/16
 * Time: 16:59
 */
namespace app\Common\Model;
use think\model;
use think\Validate;

class User extends model
{
    protected $autoWriteTimestamp = true;
    public function initialize(){
        parent::initialize();
    }

    public function checkform($post)
    {
        // 数据验证
        $validate = new Validate([
            'name|人员名称' => 'require|chsAlpha',
            'section|所属部门' => 'require|number|egt:0',
            'email|邮箱' =>  ['require','regex'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/'],
        ]);

        if (!$validate->check($post)) {
            $retArray = ['rel' => 0, 'msg' => $validate->getError()];
            return $retArray;
        }else {
            return false;
        }
    }

    public function addUser($post)
    {
        $res = $this->checkForm($post);
        if ($res) {
            return $res;
        }
        if (isset($post['id'])&&!empty($post['id'])) {//编辑
            $addinfo = $this->allowField(true)->isUpdate(true)->save($post,['id'=>$post['id']]);

        }else{//新增
            $addinfo = $this->allowField(true)->isUpdate(false)->save($post);
        }
        if ($addinfo){
            $msg['rel'] = 1;
            $msg['msg']  = '保存成功';
        }else{
            $msg['rel'] = 0;
            $msg['msg']  = '保存失败';
        }
        return $msg;
    }

    //work_date字段获取器
    public function getWorkDateAttr($value)
    {
        $detail =date('Y-m-d H:i:s',$value);
        return $detail;
    }
}